﻿--[[
@brief      游戏录像提示框Control
@date       2017-01-08
@author     jameszhai
]]

EGMRecConst =
{
    sCfgName = "first_name_by_serviceId.js";
};

--[[
local   zParam = 
{
    command="NOTIFY_VIDEO_CAPTURE_IN_GAME",
    game_id="50000010",
    data = "{\"totalVideoNum\":\"1\",\"gameId\":\"1\"}"
};

comm_center.send_broadcast("Brdcst_PluginMgr_PluginCmdArrive", zParam);
]]

TGMRecTipsCtrl = 
{
    -- 加载模块
    InitCtrl = function()
        ierd_tgp.tpf_ui.Require("Common/tgp_log.lua");
        ierd_tgp.tpf_ui.Require("game_video/view.lua");
        ierd_tgp.tpf_ui.Require("common\\OssConfig.lua");
        comm_center.reg_service("SvrPlayVideo", "TGMRecTipsCtrl.OnShowPlayer")
    end;

    -- 卸载模块
    UnInitCtrl = function()
        TPlayerView.UnInitPlayer();
        TGMRecTipsView.UnInitView();
    end;

    -- 主界面显示的时候请求JSON配置
    OnLoginOK = function(sEvtID, sJson)
        -- 拉取配置
        TGPLog.LogI("GMRecTipsCtrl", "begin get config");
        TOssConfig.QueryCfg(EGMRecConst.sCfgName, "TGMRecTipsCtrl.OnGetJsonCfg", false);
    end;

    -- 显示播放器
    OnShowPlayer = function(zParam)
        if (nil == zParam["game_id"]) or (nil == zParam["vid"]) then
            TGPLog.LogI("PlayerView", "Show player, parameter error.");
            TPlayerView.ReportPlay(-1);
            return false;
        end;

        local   sWebUrl = LoadStr("GAME_VIDEO_URL");

        sWebUrl = sWebUrl .. "&game_id=" .. tostring(zParam["game_id"]);
        sWebUrl = sWebUrl .. "&file_uuid=" .. zParam["vid"];

        -- 带上登录态
        sWebUrl = web_utility.make_login_status_url(sWebUrl, login_model.main_uin);

        local   zTemp = {};

        zTemp["url"   ] = sWebUrl;
        zTemp["width" ] = 1002;
        zTemp["height"] = 540;
        zTemp["have_scroll"] = false;
        zTemp["title"] = LoadStr("GAMEVIDEO_PLATFORM");
        zTemp["ontop"] = false;

        event_center.add_listener(browser_event.ON_TGP_BROWSER_CLOSED,
                                  "TGMRecTipsCtrl.OnBrowserClose");
        TGMRecTipsCtrl.sWebUrl = sWebUrl;

        local   sJson = base_utility.table_to_json(zTemp);
        event_center.send_event(browser_event.OPEN_TGP_BROWSER, sJson);

        TGMRecTipsCtrl.ReportPlay(0);
        TGMRecTipsCtrl.dwStart = os.clock();

        return  {};
    end;

    -- 浏览器关闭
    OnBrowserClose = function(sEvtID, sJson)
        event_center.remove_listener(browser_event.ON_TGP_BROWSER_CLOSED,
                                     "TGMRecTipsCtrl.OnBrowserClose");

        local   zParam = base_utility.json_to_table(sJson);

        if (nil ~= zParam) and (nil ~= zParam["url"]) and
           (TGMRecTipsCtrl.sWebUrl == zParam["url"]) then
            TGMRecTipsCtrl.ReportTime();
        end;

        TGMRecTipsCtrl.sWebUrl = "";
    end;

    -- 播放上报
    ReportPlay = function(nErrCode)
        -- 获取自己的UIN
        local   zTemp = ierd_tgp.tpf_ui.LuaQosDataEx:new();
        local   zData = {};

        zData["ErrCode"] = nErrCode;
        zData["UserID" ] = login_model.main_uin;

        zTemp.oc_type    = 1;
        zTemp.qos_kind   = ierd_tgp.adapt_for_imports.qos.Qos_kind.kPersonalVideo;
        zTemp.str_action = "PersonalVideo";
        zTemp.str_type   = "PlayVideo";
        zTemp.str_json   = base_utility.table_to_json(zData);

        base_utility.lua_qos_ex(zTemp);
        zTemp:delete();
    end;

    -- 上报时长
    ReportTime = function()
        if (0 ~= TGMRecTipsCtrl.dwStart) then
            local     dwTick = os.clock() - TGMRecTipsCtrl.dwStart;

            if (dwTick > 0) then
                local   zTemp = ierd_tgp.tpf_ui.LuaQosDataEx:new();
                local   zData = {};

                zData["Time"  ] = dwTick;
                zData["UserID"] = login_model.main_uin;

                zTemp.oc_type    = 1;
                zTemp.qos_kind   = ierd_tgp.adapt_for_imports.qos.Qos_kind.kPersonalVideo;
                zTemp.str_action = "PersonalVideo";
                zTemp.str_type   = "WatchTime";
                zTemp.str_json   = base_utility.table_to_json(zData);

                base_utility.lua_qos_ex(zTemp);
                zTemp:delete();
            else
                TGPLog.LogE("PlayerView", "Invalid watch time:" .. tostring(dwTick));
            end;

            TGMRecTipsCtrl.dwStart = 0;
        end;
    end;

    -- 获取到配置
    OnGetJsonCfg = function(sFile, zJson)
        --[[local zData = base_utility.json_to_table(sJson);

        if (nil == zData) or (nil == zData["name"]) then
            TGPLog.LogE("GMRecTipsCtrl", "OnGetJsonConfig, J=>T fail.");
            return;
        end;

        if (EGMRecConst.sCfgName ~= zData["name"]) then
            TGPLog.LogD("GMRecTipsCtrl", "Not my config:"..zData["name"]);
            return;
        end;

        if (nil == zData["json"]) or ("table" ~= type(zData["json"])) then
            TGPLog.LogD("GMRecTipsCtrl", "Json field is null.");
            return;
        end;

        local   zTemp = zData["json"];

        if (nil ~= zTemp) and (nil ~= zTemp["data"]) then
            TGMRecTipsCtrl.zName2ID = zTemp["data"];
        else
            TGPLog.LogE("GMRecTipsCtrl", "Table error.");
        end;--]]

        if (nil == zJson) or (nil == sFile) or (EGMRecConst.sCfgName ~= sFile) then
            TGPLog.LogI("GMRecTipsCtrl", "OnGetJsonConfig,  get json fail.");
            return;
        end;

        local zData = zJson["data"];
        if (nil == zData) then
            TGPLog.LogE("GMRecTipsCtrl", "data table error.");
            return;
        end;
        TGMRecTipsCtrl.zName2ID = zData;
    end;

    -- 根据ServiceID找游戏ID
    FindGameID = function(nSvrID)
        if (nil ~= TGMRecTipsCtrl.zName2ID) then
            for nIdx = 1, #TGMRecTipsCtrl.zName2ID do
                local   zTemp = TGMRecTipsCtrl.zName2ID[nIdx];
                
                if  (nil ~= zTemp["service_id"]) and (nil ~= zTemp["game_id"]) and 
                    (nSvrID == tonumber(zTemp["service_id"])) then
                    return tonumber(zTemp["game_id"]);
                end;
            end;

            TGPLog.LogI("GMRecTipsCtrl", "Service ID not find:"..tostring(nSvrID));
        else
            TGPLog.LogI("GMRecTipsCtrl", "Table is null:"..tostring(nSvrID));
        end;

        return nSvrID;
    end;
    
    -- 收到游戏内已截图Tips
    ReceiveScreenShotNotify = function(data)
        if data.command == "NOTIFY_SCREENSHOT_IN_GAME" then
            base_utility.log("ReceiveScreenShotNotify"..data.param);

            local param = base_utility.json_to_table(data.param);
            if param ~= nil and param.game_id ~= nil then
                local game_id = tonumber(param.game_id);

                game_id = TGMRecTipsCtrl.FindGameID(game_id);

                if game_id ~= nil and TGMRecTipsCtrl.record_info_list[tostring(game_id)] ~= nil and 
                   param.total_count ~= nil then
                    TGMRecTipsCtrl.record_info_list[tostring(game_id)].shot_num = tonumber(param.total_count);
                end;
            end;
        end;
    end;

    -- 收到游戏内录制视频
    ReceiveRecordVideoNotify = function(data)
        if data.command == "NOTIFY_VIDEO_CAPTURE_IN_GAME" then
            base_utility.log("GameVideCtrl"..data.param);

            local param = base_utility.json_to_table(data.param);

            if (nil == param or nil == param.gameId or nil == tonumber(param.gameId)) then
                TGPLog.LogE("GameVideCtrl", "Parser json fail.");
                return;        
            end;

            local   service_id = tonumber(param.gameId);
            TGPLog.LogD("GameVideCtrl", "service id:"..tostring(service_id));

            -- ServiceID => GameID
            local game_id = TGMRecTipsCtrl.FindGameID(tonumber(service_id));
            TGPLog.LogD("GameVideCtrl", "After convert:"..tostring(game_id));
            TGPLog.LogD("GameVideCtrl", 
                        "VideoNo:" .. tostring(param.totalVideoNum) ..
                        " GameID:" .. tostring(game_id));

            -- 调试
            -- TGMRecTipsCtrl.record_info_list[tostring(game_id)] = {};

            if param ~= nil and game_id ~= nil and nil ~= param.totalVideoNum then
                if TGMRecTipsCtrl.record_info_list[tostring(game_id)] ~= nil then
                    if tonumber(param.totalVideoNum) == 0 then
                        TGMRecTipsCtrl.record_info_list[tostring(game_id)].video_num = 1;
                    else
                        TGMRecTipsCtrl.record_info_list[tostring(game_id)].video_num = tonumber(param.totalVideoNum);
                    end;
                else
                    TGPLog.LogI("GameVideoCtrl", "Game not started: " .. tostring(game_id));
                end;
            end;
            
            if TGMRecTipsCtrl.record_info_list[tostring(game_id)] ~= nil and
               TGMRecTipsCtrl.record_info_list[tostring(game_id)].video_num > 0 then
                TGMRecTipsCtrl.ShowTips(game_id);
            else
                TGPLog.LogI("GameVideoCtrl", "Wait for game start: " .. tostring(game_id));
            end;
        end;
    end;

    onGameStart = function(event_id, json_str)
        if json_str == nil then
            return;
        end;
        local json_table = base_utility.json_to_table(json_str);
        if json_table == nil or json_table.game_id == nil or json_table.uin == nil then
            return;
        end;
        local uin = tonumber(json_table.uin);
        local game_id = tonumber(json_table.game_id);
        if game_id ~= nil then
            game_id = TGMRecTipsCtrl.FindGameID(game_id);

            local record_info = {game_id = 0, shot_num = 0, video_num = 0, icon = "", tick_show = false};
            record_info.game_id = game_id;
            local zTemp = {};
            zTemp["GameID"] = game_id;
            local zInfo = comm_center.call_service("GetGameIconByID_Svr", zTemp);
            if (nil ~= zInfo) and (nil ~= zInfo["Icon"]) then
                record_info.icon = zInfo["Icon"];
            else
                record_info.icon = "";
            end;

            record_info.shot_num = 0;
            record_info.video_num = 0;
            TGMRecTipsCtrl.record_info_list[tostring(game_id)] = record_info;
            
            TGPLog.LogI("GameVideoCtrl", "Game start: " .. tostring(game_id));
        end
    end;

    onGameEnd = function(event_id, json_str)
        if json_str == nil then
            return;
        end;
        local json_table = base_utility.json_to_table(json_str);
        if json_table == nil or json_table.game_id == nil then
            return;
        end;

        local game_id = tonumber(json_table.game_id);

        game_id = TGMRecTipsCtrl.FindGameID(game_id);

        TGPLog.LogI("GameVideoCtrl", "Game stop: " .. tostring(game_id));
        
        if game_id ~= nil and TGMRecTipsCtrl.record_info_list[tostring(game_id)] ~= nil then
            if TGMRecTipsCtrl.record_info_list[tostring(game_id)].shot_num > 0 then
                TGMRecTipsCtrl.TickToShowTips(game_id);
            end;
        end;
    end;

    -- 延迟显示tips窗口，等待录制视频消息通知
    TickToShowTips = function(game_id)
        if TGMRecTipsCtrl.record_info_list[tostring(game_id)] ~= nil and
           TGMRecTipsCtrl.record_info_list[tostring(game_id)].tick_show == false then
            local tpfui =
                base_utility.get_component_adapter("ITpf_ui",
                    "ierd_tgp.tpf_ui.GetTPFUI();");
            if nil ~= tpfui then
                tpfui:AddInterval("TGMRecTipsCtrl.ShowTips", 2*1000, tonumber(game_id));
                TGMRecTipsCtrl.record_info_list[tostring(game_id)].tick_show = true;
                base_utility.log("[TGMRecTipsCtrl]TickToShowTips:"..tostring(game_id));
            end;
        end;
    end;

    EndTick = function(game_id)
        if TGMRecTipsCtrl.record_info_list[tostring(game_id)] ~= nil and
           TGMRecTipsCtrl.record_info_list[tostring(game_id)].tick_show == true then
            local tpfui =
                base_utility.get_component_adapter("ITpf_ui",
                    "ierd_tgp.tpf_ui.GetTPFUI();");
            if nil ~= tpfui then
                tpfui:RemoveInterval("TGMRecTipsCtrl.ShowTips", tonumber(game_id));
                TGMRecTipsCtrl.record_info_list[tostring(game_id)].tick_show = false;
                base_utility.log("[TGMRecTipsCtrl]EndTick");
            end;
        end;
    end;
    
    ShowTips = function(game_id)
        base_utility.log("[TGMRecTipsCtrl]ShowTips:"..tostring(game_id));
        TGMRecTipsCtrl.EndTick(game_id);
        if TGMRecTipsCtrl.record_info_list[tostring(game_id)] ~= nil then
            if (TGMRecTipsView.InitView()) then
                TGMRecTipsView.ShowView(TGMRecTipsCtrl.record_info_list[tostring(game_id)]);
            end;
            TGMRecTipsCtrl.record_info_list[tostring(game_id)] = nil;
        end
    end;

    zName2ID = nil;
    dwStart  = 0;
    sWebUrl  = "";
    record_info_list = {};
};

-- 自动加载
TGMRecTipsCtrl.InitCtrl();